﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public enum CardState
{
    Charged,Releasing
}


public class FallMagicCard : AbstractMagicCard {
    public GameObject MagicEffectPrefab;
    public BeamRay beamRay;
    public VRControllerInteractive controllerInteractive;
    public Rigidbody rb;
    public AbstractCardTriggerAssessor triggerAssessor;



    private void Start()
    {
        controllerInteractive.StartHolding += HoldingControllerAddAction;
        controllerInteractive.ReleaseHolding += HoldingControllerReleaseAction;
        rb.isKinematic = true;
    }

    // Update is called once per frame
    void Update () {
        if(state==CardState.Charged && controllerInteractive.isHolding)
        {
            beamRay.isShoot = true;
            transform.position = controllerInteractive.controller.transform.position;
        }
        else
        {
            beamRay.isShoot = false;
        }
		
	}

    public void ControllerPadClicked(object sender, ClickedEventArgs e)
    {
        if(state == CardState.Charged && (triggerAssessor==null || triggerAssessor.AbleTrigger()))
        {
            state = CardState.Releasing;
            beamRay.isShoot = false;
            rb.isKinematic = false;
            controllerInteractive.Release();
            
        }
    }
    
    public void ControllerPadUnClicked(object sender,ClickedEventArgs e)
    {
        
    }

    protected override void HoldingControllerAddAction()
    {
        controllerInteractive.controller.PadClicked += ControllerPadClicked;
        controllerInteractive.controller.PadUnclicked += ControllerPadUnClicked;
    }

    protected override void HoldingControllerReleaseAction()
    {
        controllerInteractive.controller.PadClicked -= ControllerPadClicked;
        controllerInteractive.controller.PadUnclicked -= ControllerPadUnClicked;
    }


    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.layer == LayerMask.NameToLayer(LayerNameSet.Terrain))
        {
            Instantiate(MagicEffectPrefab, transform.position, transform.rotation);
            Destroy(this.gameObject);
        }
    }


}
